package cn.thr.pethome.util;

import cn.thr.pethome.constants.BaseConstants;
import cn.thr.pethome.shop.domain.ShopAuditLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.mail.internet.MimeMessage;

@Component
    public class EmailUtil {

        //固定的
        @Autowired
        private JavaMailSender javaMailSender;

                                                    //<T>
    public Boolean FromEmail(String email,Long id, ShopAuditLog shopAuditLog) {
        try {
            //创建复杂邮件对象
            MimeMessage mimeMessage = javaMailSender.createMimeMessage();
            //发送复杂邮件的工具类
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
            helper.setFrom("1741115987@qq.com");
            //helper.setSubject("这是图片");
            //第二个参数 true 代表是否是一个html内容
            if(shopAuditLog.getState() == BaseConstants.Shop.STATE_WAIT_ACTIVE){
                helper.setText("<h1>点击激活你的店铺</h1>"+
                        "<a href='http://localhost:8080/shop/activation/"+id+"'>点击激活账号</a>",true);
            }else if (shopAuditLog.getState() == BaseConstants.Shop.STATE_REJECT_AUDIT){
                helper.setText("<h1>申请失败</h1>"+
                        "<h5>你申请失败原因是["+shopAuditLog.getNote()+"]</h5>",true);
            }
            helper.setTo(email);
            //发送邮件
            javaMailSender.send(mimeMessage);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
            }
        }
    }

